﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.Configuration;

namespace AutoNyaa.Logic.Entities
{
    [Serializable]
    public class Filter : IEquatable<Filter>, IEquatable<Guid>
    {
        private string _filterRegex;
        [XmlIgnore]
        public Guid Id { get; set; }

        public string Title { get; set; }
        public string SearchString { get; set; }
        public string FilterRegex
        {
            get
            {
                return _filterRegex;
            }
            set
            {
                RegexStringValidator = new RegexStringValidator(value);
                _filterRegex = value;
            }
        }
        public ListItem Category { get; set; }
        public ListItem TypeFilter { get; set; }
        public DateTime LastUpdated { get; set; }

        [XmlIgnore]
        public RegexStringValidator RegexStringValidator { get; set; }

        public Filter()
        {
            Id = Guid.NewGuid();
        }

        public bool Equals(Filter other)
        {
            if (Id == other.Id)
            {
                return true;
            }
            return false;
        }

        public bool Equals(Guid other)
        {
            if (Id == other)
            {
                return true;
            }
            return false;
        }

        public RssFeed GetRssFeed(string url, int page = 1)
        {
            return RssFeed.Deserialize(new Uri(url), page, SearchString, Category.Value, TypeFilter.Value);
        }

        public bool Validates(string text)
        {
            try
            {
                RegexStringValidator.Validate(text);
                return true;
            }
            catch (ArgumentException e)
            {
                return false;
            }
        }
    }
}